package ld.servlet;

import com.alibaba.fastjson.JSON;
import ld.pojo.Dept;
import ld.service.DeptService;
import ld.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Liudao
 * @Date: 2022-08-01-22:09
 * @Description:
 */
@WebServlet("/dept")
public class DeptServlet extends HttpServlet {
    private DeptService deptService;

    @Override
    public void init() throws ServletException {
        deptService = new DeptServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 处理中文乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        //2. 得到所有的部门列表
        List<Dept> deptList = deptService.findAll();
        //3. 输出到页面
        resp.getWriter().println(JSON.toJSONString(deptList));
        resp.getWriter().flush();
    }
}
